﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.EntitiesInfo;
namespace Web.Cms.Questions
{
    public partial class AddEditNewPartner : AdminPage
    {
        private int id = 0;
        #region Page_load
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                id = ConvertUtility.ToInt32(Request.QueryString["id"]) == null ? 0 : ConvertUtility.ToInt32(Request.QueryString["id"]);
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
            catch (Exception)
            {

                throw;
            }

        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (id > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    PartnerInfo info = PartnerBL.GetInfo(id);
                    if (info != null)
                    {
                        txtName.Text = info.Name.ToString();
                        txtDescription.Text = info.Description.ToString();
                        dropStatus.SelectedValue = info.Status.ToString();
                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

        #region ClearText
        void ClearText()
        {
            txtName.Text = "";
            txtDescription.Text = "";

        }

        #endregion

        #region FillData
        void FillData(PartnerInfo info)
        {

            info.Id = id;
            info.Name = txtName.Text;
            info.Description = txtDescription.Text;
            info.CreatedDate = DateTime.Now;
            info.Status = ConvertUtility.ToInt32(dropStatus.SelectedValue);

        }
        #endregion

        #region Thêm mới, chỉnh sửa, hủy
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Length == 0)
                {
                    MessageBox.Show("Bạn chưa nhập tên đối tác !");
                    txtName.Focus();
                    return;
                }
                PartnerInfo info = new PartnerInfo();
                FillData(info);
                int i = PartnerBL.Insert(info);
                if (i > 0)
                {
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();
                }
                else
                {
                    lblUpdateStatus.Text = "Đã có lỗi xảy ra";
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                PartnerInfo info = new PartnerInfo();
                FillData(info);
                PartnerBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công !";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=PartnersManager';</script>");
        }
        #endregion     
    }
}